<!-- 模板 -->
<template>
  <div class="classification">
      <div class="box">
          <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-bottom: 20px;"
          size="small"
          @click="xinzeng"
          >新增</el-button>
        <el-card shadow="hover">
            <el-table  v-loading="loading" :data="tableData" border >
                <el-table-column type="index" label="序号" align="center" width="80px"/>
                <el-table-column prop="name" label="分类名称" align="center" />
                <!-- <el-table-column prop="createTime" label="创建时间" align="center" /> -->
                <el-table-column  label="操作"  align="center">
                  <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" @click="bianji(scope.row)" size="small">编 辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" @click="shanchu(scope.row)" size="small">删 除</el-button>
                  </template>
                </el-table-column>
            </el-table>
        </el-card>
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="params.page"
          :limit.sync="params.limit"
          @pagination="getList"
        />
      </div>

<!-- 弹窗 -->
<el-dialog
  :title="title"
  :visible.sync="dialogVisible"
  width="30%">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分类名称:" prop="name">
            <el-input v-model="form.name"></el-input>
        </el-form-item>
    </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="onSubmit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { getXinzeng,getClassList,delClass,updateClass,getRegistration} from "@/api/order/orderschool";
export default {
  name: '',
  data () {
    return {
        tableData:[],
        params:{
            page: 1,
            limit: 10,
        },
        pageTotal: 0,
        dialogVisible:false,
        title:'',
        form:{},
        rules:{
          name: [
            { required: true, message: '请输入名称', trigger: 'blur' },
          ],
        },
    }
  },
  props: [],
  components: {},
  computed: {},
  filters: {},
  methods: {
   
      bianji(row){
        this.title = '编辑'
        this.form = row
        this.dialogVisible = true
      },
      shanchu(row){
        const id = row.id
       this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delClass(id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
      },
      xinzeng(){
          this.title = '新增'
          this.form = {}
          this.dialogVisible = true
      },
      onSubmit(){
          this.$refs["form"].validate(valid => {
        if (this.form.id != undefined) {
                  updateClass(this.form).then(res=>{
                      if(res.code == 200){
                          this.$message.success('编辑成功')
                          this.dialogVisible = false
                          this.getList()
                      }
                  })
                } else if(valid){
                  getXinzeng(this.form).then(res=>{
                      // console.log(res,'。。。。。。。。。')
                      if(res.code == 200){
                          this.$message.success('添加成功')
                          this.dialogVisible = false
                          this.getList()
                      }
                  })
                }
      });
      },
      // 获取所有
      getList() {
          this.loading = true;
          getClassList(this.params).then(res => {
            this.tableData = res.data.list
            // console.log(this.tableData);
            this.pageTotal = res.data.total
            this.loading = false;
          });
        },
  },
  created () {
    
    this.getList()
    // this.getRegistrationList()
  },
  mounted () {}
}
</script>

<style scoped>
.box {
  padding: 20px 10px;
}
</style>
